package cn.jisol.ngame.sync.state;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.jisol.ngame.proto.JNSyncMessage;

public class JNSyncStateDefault extends JNSyncState {
    @Override
    protected JNSyncMessage.JNStateData Merge(JNSyncMessage.JNStateData oldData, JNSyncMessage.JNStateData newData) {

        if (newData.getData().isEmpty()) return oldData;
        if (newData.getData().size() == 1) return oldData;

        try {
            JSONObject newValue = JSONUtil.parseObj(newData.getData().toStringUtf8());
            JSONObject oldValue = JSONUtil.parseObj(oldData.getData().toStringUtf8());

            oldValue.keySet().forEach(key -> {
                if (newValue.containsKey(key) && newValue.get(key) != null) {
                    oldValue.set(key, newValue.get(key));
                }
            });
        } catch (Exception e) {
            //打印错误
            System.out.println(e.getMessage());
        }

        return oldData;

    }
}
